package com.ruoyi.central.controller;

import com.ruoyi.central.constant.CentralSysConfigConstants;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname IndexController
 * @Author: sunluquan
 * @Date: 2022/2/28 10:57
 * @Description TODO
 * 集成控制台
 */
@Controller
@RequestMapping("/central")
@Slf4j
public class IndexController {

    private String prefix = "central";

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 总控台首页
     * @param mmap
     * @return
     */
    @GetMapping("/index")
    public String index(ModelMap mmap){

        if(permissionService.isPermitted("central:control:view")){

            Long centralControllerMenuId = Convert.toLong(sysConfigService.selectConfigByKey(CentralSysConfigConstants.CENTRAL_CONTROLLER_MANAGER_MENU_KEY));

            if(StringUtils.isNotNull(centralControllerMenuId)){

                SysMenu centralControllerMenu = sysMenuService.selectMenuById(centralControllerMenuId);

                if(StringUtils.isNotNull(centralControllerMenu) && centralControllerMenu.visible()){

                    SysMenu parentMenu = new SysMenu();
                    parentMenu.setParentId(centralControllerMenuId);

                    List<SysMenu> sysMenus = sysMenuService.selectMenuList(parentMenu, ShiroUtils.getUserId()).stream()
                            .filter(menu->SysMenu.CATALOG_TYPE.equals(menu.getMenuType())||SysMenu.MENU_TYPE.equals(menu.getMenuType())).collect(Collectors.toList());

                    mmap.put("menus",sysMenus);

                    int line = 0;//设置展示的行数
                    int column=4;//设置每行展示多少个
                    if(StringUtils.isNotEmpty(sysMenus)){
                        line = sysMenus.size()%column==0?sysMenus.size()/column:(sysMenus.size()/column)+1;
                    }
                    mmap.put("line",line);
                    mmap.put("column",column);

                    return prefix + "/index";
                }
            }
        }
        return "redirect:/index";
    }

    @GetMapping("/processFlowMonitoring")
    public String processFlowMonitoring() {
        return prefix + "/processFlowMonitoring";
    }

    @GetMapping("/systemHomePage")
    public String systemHomePage() {
        return prefix + "/systemHomePage";
    }

    @GetMapping("/processing")
    public String processing() {
        return prefix + "/processing";
    }

    @GetMapping("/stationSelection")
    public String stationSelection() {
        return prefix + "/stationSelection";
    }

    @GetMapping("/aGVConditionMonitoring")
    public String aGVConditionMonitoring() {
        return prefix + "/aGVConditionMonitoring";
    }
}
